package com.zan.handler;

import com.alibaba.fastjson.JSON;
import com.zan.utils.WebUtils;
import com.zan.vo.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Zan
 * @Date 2023/10/18 14:33
 * @Description : 自定义处理授权过程中的异常的类
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 403 - 未授权的授权码
        ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "您的权限不足");
        String jsonStr = JSON.toJSONString(result);
        // 处理认证过程中的异常
        WebUtils.renderString(response, jsonStr);
    }
}
